import { defineStore } from 'pinia'
import { ref } from 'vue'
import { RoleMenus } from '@/api/common'
export const useMenusStore = defineStore('menus', () => {
  /** 菜单 */
  const menus = ref<Menu[]>()
  /** 当前页面 */
  const currentPage = ref<any>(null)
  /** 历史页面 */
  const historyPage = ref<HistoryPageMenu[]>([])
  const getMenus = (): any => {
    return RoleMenus().then((res:ServiceResponse<Menu[]>) => {
      menus.value = res.data
      return res.data
    })
  }
  /** 设置历史页面 */
  const setHistoryPage = (route: any):void => {
    currentPage.value =route
    if(route.fullPath === '/login') return
    if(route.fullPath === '/home') return
    if(historyPage.value?.some((item:HistoryPageMenu) => item.path === route.fullPath)) return
    historyPage.value?.push({name:route.meta.title,path:route.fullPath})
  }

  return { menus,currentPage,historyPage,getMenus,setHistoryPage }
})